import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../controller/auth_controller.dart';
import '../../http/http_agent.dart';
import '../../routes/app_routes.dart';

class StartupAuthPage extends StatefulWidget {
  @override
  _StartupAuthPageState createState() => _StartupAuthPageState();
}

class _StartupAuthPageState extends State<StartupAuthPage> {
  final AuthController authController = Get.find();

  @override
  void initState() {
    super.initState();
    _checkLoginStatus();
  }

  Future<void> _checkLoginStatus() async {
    try {
      // 调用登录API检查
      final success = await authController.checkStartupLogin();

      if (success) {
        print("========");
        // 登录成功，进入应用
        Get.offAllNamed(AppRoutes.root);
      } else {
        // 登录失败，可以选择重试或跳转到登录页面
        // 这里可以根据需要添加重试逻辑或直接跳转
        _checkLoginStatus(); // 简单重试
      }
    } catch (e) {
      // 网络错误，重试
      Future.delayed(Duration(seconds: 2), () {
        _checkLoginStatus();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          color: Color(0xFF1A1A1A),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 优化的加载动画容器
              Container(
                width: 80,
                height: 80,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.white.withOpacity(0.1),
                  border: Border.all(
                    color: Colors.white.withOpacity(0.2),
                    width: 1,
                  ),
                ),
                child: Center(
                  child: SizedBox(
                    width: 40,
                    height: 40,
                    child: CircularProgressIndicator(
                      valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                      strokeWidth: 3.0,
                      strokeCap: StrokeCap.round,
                    ),
                  ),
                ),
              ),
              SizedBox(height: 32),
              // 加载提示文字
              Text(
                'Authentication in progress',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 0.5,
                ),
              ),
              SizedBox(height: 12),
              // 副标题
              Text(
                'Please wait...',
                style: TextStyle(
                  color: Colors.white.withOpacity(0.7),
                  fontSize: 14,
                  letterSpacing: 0.3,
                ),
              ),
              SizedBox(height: 40),
              // 云挖矿免责声明
              Container(
                margin: EdgeInsets.symmetric(horizontal: 32),
                padding: EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.05),
                  borderRadius: BorderRadius.circular(12),
                  border: Border.all(
                    color: Colors.white.withOpacity(0.1),
                    width: 1,
                  ),
                ),
                child: Column(
                  children: [
                    Icon(
                      Icons.cloud_outlined,
                      color: Colors.white.withOpacity(0.8),
                      size: 24,
                    ),
                    SizedBox(height: 8),
                    Text(
                      'cloud_mining_disclaimer'.tr,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.white.withOpacity(0.8),
                        fontSize: 12,
                        height: 1.4,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
